/**
 * @filename:UserOrderInfoServiceImpl 2019-01-17 14:56:43
 * @project skz  V1.3.3
 * Copyright(c) 2018 Liu,Wenzhao Co. Ltd. 
 * All right reserved. 
 */
package com.bingsum.service;

import com.bingsum.annotation.Api;
import com.bingsum.mapper.OrderPackageInfoMapper;
import com.bingsum.model.OrderPackageInfo;
import com.bingsum.model.User;
import com.bingsum.util.ApiUtil;
import com.bingsum.util.ParaData;
import com.bingsum.util.RandomUtils;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;

import java.util.List;

import com.bingsum.model.UserOrderInfo;
import com.bingsum.mapper.UserOrderInfoMapper;
import tk.mybatis.mapper.entity.Example;

/**   
 *  
 * @Description:  用户订单信息——SERVICE
 * @Author:       Liu,Wenzhao   
 * @CreateDate:   2019-01-17 14:56:43
 * @Version:      V1.3.3
 *    
 */
@Service("userOrderInfoService")
@Transactional(readOnly = true)
public class UserOrderInfoService{

    @Autowired
    private UserOrderInfoMapper userOrderInfoMapper;
    @Autowired
    private OrderPackageInfoMapper orderPackageInfoMapper;
    @Autowired
    private UserService userService;

    @Api
    public Object getOrderInfoList(ParaData pd) {
        User user =this.userService.findByOpenId(pd.getString("openId"));
        Page<?> page = PageHelper.startPage(pd.getInteger("currentPage"), 20);
        Example example = new Example(UserOrderInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", pd.getString("status"));
        criteria.andEqualTo("userId",user.getId());
        example.orderBy("createTime").desc();
        userOrderInfoMapper.selectByExample(example);
        return ApiUtil.returnObject(pd, page);
    }

    @Api
    public Object getAdminOrderInfoList(ParaData pd) {
        Page<?> page = PageHelper.startPage(pd.getInteger("currentPage"), 20);
        userOrderInfoMapper.getAdminOrderInfoList(pd);
        return ApiUtil.returnObject(pd, page);
    }

    @Api
    public List<UserOrderInfo> getOrderInfoSearch(ParaData pd) {
        Example example = new Example(UserOrderInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if(pd.getString("origin") != null){
            criteria.andLike("origin", "%"+pd.getString("origin") + "%");
        }
        if(pd.getString("destination") != null){
            criteria.andLike("destination", "%"+pd.getString("destination") + "%");
        }
        List<UserOrderInfo> countries = userOrderInfoMapper.selectByExample(example);
        return countries;
    }

    @Api
    public ParaData getOrderInfoByTime(ParaData pd) {
        return userOrderInfoMapper.getOrderInfoByTime(pd);
    }

    @Api
    public Object getOrderInfo(ParaData pd) {
        ParaData res = userOrderInfoMapper.getOrderInfo(pd);
        if (res == null){
            return ApiUtil.returnDescFail(pd,"无返回");
        }
        return res;
    }

    @Api
    @Transactional(readOnly = false)
    public Object newOrderInfoPsg(ParaData pd) {
        UserOrderInfo userOrderInfo = pd.toAddBean(UserOrderInfo.class);
        UserService userService  = pd.toBean(UserService.class);
        userOrderInfo.setUid(RandomUtils.getRandomNumberStringWithTime(2,null));
        User user =this.userService.findByOpenId(pd.getString("openId"));
        userOrderInfo.setUserId(user.getId());
        this.userOrderInfoMapper.insert(userOrderInfo);
        return ApiUtil.returnOK(pd,userOrderInfo);
    }

    @Api
    @Transactional(readOnly = false)
    public Object newOrderInfoPag(ParaData pd) {
        UserOrderInfo userOrderInfo = pd.toAddBean(UserOrderInfo.class);
        UserService userService  = pd.toBean(UserService.class);
        userOrderInfo.setUid(RandomUtils.getRandomNumberStringWithTime(2,null));
        User user =this.userService.findByOpenId(pd.getString("openId"));
        userOrderInfo.setUserId(user.getId());
        this.userOrderInfoMapper.insert(userOrderInfo);

        OrderPackageInfo pack = pd.toAddBean(OrderPackageInfo.class);
        pack.setUserOrderInfoId(userOrderInfo.getId());
        this.orderPackageInfoMapper.insert(pack);

        return ApiUtil.returnOK(pd,userOrderInfo);
    }

    @Api
    @Transactional(readOnly = false)
    public Object setOrderInfo(ParaData pd) {
        UserOrderInfo userOrderInfo = pd.toBean(UserOrderInfo.class);
        this.userOrderInfoMapper.updateByPrimaryKeySelective(userOrderInfo);
        return ApiUtil.returnOK(pd,userOrderInfo);
    }

    @Api
    @Transactional(readOnly = false)
    public Object setOrderStatus(ParaData pd) {
        User user =this.userService.findByOpenId(pd.getString("openId"));
        UserOrderInfo param = new UserOrderInfo();
        param.setUserId(user.getId());
        param.setUid(pd.getString("uid"));
        UserOrderInfo order = this.userOrderInfoMapper.selectOne(param);

        if(order == null){
            return ApiUtil.returnDescFail(pd, "无法找到订单");
        }
        order.setStatus(3);
        this.userOrderInfoMapper.updateByPrimaryKeySelective(order);
        return ApiUtil.returnOK(pd,order);
    }


}